<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="产品" prop="productGgId">
									<el-input v-model="form.productGgId" placeholder="请输入产品" clearable></el-input>
								</el-form-item>
								<el-form-item label="最小单位 完整: 1箱,每箱20袋， 每袋10包，每包10小包" prop="minimumName">
									<el-input v-model="form.minimumName" placeholder="请输入最小单位 完整: 1箱,每箱20袋， 每袋10包，每包10小包" clearable></el-input>
								</el-form-item>
								<el-form-item label="最小单位价格" prop="minimumPrice">
									<el-input v-model="form.minimumPrice" placeholder="请输入最小单位价格" clearable></el-input>
								</el-form-item>
								<el-form-item label="箱" prop="name1">
									<el-input v-model="form.name1" placeholder="请输入箱" clearable></el-input>
								</el-form-item>
								<el-form-item label="包 瓶" prop="name2">
									<el-input v-model="form.name2" placeholder="请输入包 瓶" clearable></el-input>
								</el-form-item>
								<el-form-item label="数量" prop="number2">
									<el-input v-model="form.number2" placeholder="请输入数量" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="name3">
									<el-input v-model="form.name3" placeholder="请输入" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="number3">
									<el-input v-model="form.number3" placeholder="请输入" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="name4">
									<el-input v-model="form.name4" placeholder="请输入" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="number4">
									<el-input v-model="form.number4" placeholder="请输入" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="name5">
									<el-input v-model="form.name5" placeholder="请输入" clearable></el-input>
								</el-form-item>
								<el-form-item label="" prop="number5">
									<el-input v-model="form.number5" placeholder="请输入" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import purchaseAPI from '@/api/purchase/productGgUnit';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: '最多5层， 1箱,每箱20袋， 每袋10包，每包10小包  产品采购时，自动保存。下次采购时，界面可以自动联想导入。'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					productGgId: "",
					minimumName: "",
					minimumPrice: "",
					name1: "",
					name2: "",
					number2: "",
					name3: "",
					number3: "",
					name4: "",
					number4: "",
					name5: "",
					number5: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await purchaseAPI.productGgUnit.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.productGgId = data.productGgId;
				this.form.minimumName = data.minimumName;
				this.form.minimumPrice = data.minimumPrice;
				this.form.name1 = data.name1;
				this.form.name2 = data.name2;
				this.form.number2 = data.number2;
				this.form.name3 = data.name3;
				this.form.number3 = data.number3;
				this.form.name4 = data.name4;
				this.form.number4 = data.number4;
				this.form.name5 = data.name5;
				this.form.number5 = data.number5;
			}
		}
	}
</script>
<style>
</style>
